package org.jdesktop.swingx.table.editor;

import org.swing.datechooser.beans.TimeEditor;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.sql.Timestamp;
import java.util.EventObject;

/**
 * Created by babitsky.viacheslav
 * Date: 05.03.2008
 * Time: 20:12:28
 */
public class TimeCellEditor extends AbstractCellEditor implements TableCellEditor, TreeCellEditor {
//
//  Instance Variables
//

    /**
     * The Swing component being edited.
     */
    protected TimeEditor editorComponent;
    /**
     * An integer specifying the number of clicks needed to start editing.
     * Even if <code>clickCountToStart</code> is defined as zero, it
     * will not initiate until a click occurs.
     */
    protected int clickCountToStart = 2;

    public TimeCellEditor() {
        editorComponent = new TimeEditor();
        editorComponent.addActionListener(new ActionListener() {

            /**
             * Invoked when an action occurs.
             */
            public void actionPerformed(ActionEvent e) {
                stopCellEditing();
            }
        });

    }

    /**
     * Returns the value contained in the editor.
     *
     * @return the value contained in the editor
     */
    public Object getCellEditorValue() {
        return editorComponent.getTimestamp();
    }

    /**
     * Sets an initial <code>value</code> for the editor.  This will cause
     * the editor to <code>stopEditing</code> and lose any partially
     * edited value if the editor is editing when this method is called. <p>
     * <p/>
     * Returns the component that should be added to the client's
     * <code>Component</code> hierarchy.  Once installed in the client's
     * hierarchy this component will then be able to draw and receive
     * user input.
     *
     * @param table      the <code>JTable</code> that is asking the
     *                   editor to edit; can be <code>null</code>
     * @param value      the value of the cell to be edited; it is
     *                   up to the specific editor to interpret
     *                   and draw the value.  For example, if value is
     *                   the string "true", it could be rendered as a
     *                   string or it could be rendered as a check
     *                   box that is checked.  <code>null</code>
     *                   is a valid value
     * @param isSelected true if the cell is to be rendered with
     *                   highlighting
     * @param row        the row of the cell being edited
     * @param column     the column of the cell being edited
     * @return the component for editing
     */
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        editorComponent.setTimestamp((Timestamp) value);
        editorComponent.setCaretPosition(0);
        return editorComponent;
    }

    /**
     * Sets an initial <I>value</I> for the editor.  This will cause
     * the editor to stopEditing and lose any partially edited value
     * if the editor is editing when this method is called. <p>
     * <p/>
     * Returns the component that should be added to the client's
     * Component hierarchy.  Once installed in the client's hierarchy
     * this component will then be able to draw and receive user input.
     *
     * @param tree       the JTree that is asking the editor to edit;
     *                   this parameter can be null
     * @param value      the value of the cell to be edited
     * @param isSelected true if the cell is to be rendered with
     *                   selection highlighting
     * @param expanded   true if the node is expanded
     * @param leaf       true if the node is a leaf node
     * @param row        the row index of the node being edited
     * @return the component for editing
     */
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
        editorComponent.setTimestamp((Timestamp) value);
        editorComponent.setCaretPosition(0);
        return editorComponent;
    }

    /**
     * Returns true.
     *
     * @param anEvent an event object
     * @return true
     */
    @Override
    public boolean isCellEditable(EventObject anEvent) {
        if (anEvent instanceof MouseEvent) {
            return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
        }
        return true;
    }
}